#include "StdAfx.h"
#include "Utility.h"
#include "CustomButton.h"

#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")
using namespace Gdiplus;

IMPLEMENT_DYNAMIC(CCustomButton, CButton)

CCustomButton::CCustomButton(void)
{
	//common Parameter
	m_nState = BTN_NORMAL;

	//String and Font Parameter
	m_strText=_T("");
	m_nFontSize = 12;
	m_nFontStyle = FontStyleBold;
	m_enFontUnit = UnitPoint;
	m_strTextFormat = NULL;
	m_pFontFamily = NULL;
	m_pFontBrush = NULL;
	m_pFont = NULL;

	//Image and Handle Parameter
	m_hBgDC = m_hDrawDC = NULL;
	m_hPParentWnd = m_hParentWnd = NULL;
	for(int i = 0; i < 4; ++i)
		m_pImg[i] = NULL;
}

CCustomButton::~CCustomButton(void)
{
	if(m_pFont != NULL){ delete m_pFont;  m_pFont = NULL; }
	if(m_pFontBrush != NULL){ delete m_pFontBrush;  m_pFontBrush = NULL; }
	if(m_pFontFamily != NULL){ delete m_pFontFamily;  m_pFontFamily = NULL; }
	if(m_strTextFormat != NULL){ delete m_strTextFormat;  m_strTextFormat = NULL; }	

	for(int i = 0; i < 4; ++i)
		if ( m_pImg[i] )
			delete m_pImg[i];
}

/*********************************************************************************************************************/
/*********************************************************************************************************************/
//Common Setting
/*********************************************************************************************************************/
/*********************************************************************************************************************/
void CCustomButton::SetState(int nState)
{
	if(m_nState != nState){ m_nState = nState;  Invalidate(); }
}

BOOL CCustomButton::EnableWindow(BOOL bEnable)
{
	if(bEnable)  SetState(1);
	else         SetState(0);

	return CButton::EnableWindow(bEnable);
}

void CCustomButton::Initialize(HWND hPParentWnd, HWND hParentWnd, HDC hBgDC, HDC hDrawDC, int nParentx, int nParenty)
{
	m_hPParentWnd = hPParentWnd;
	m_hParentWnd = hParentWnd;

	m_hBgDC = hBgDC;
	m_hDrawDC = hDrawDC;

	::GetWindowRect(m_hPParentWnd, m_rtPPRect);
	::GetWindowRect(m_hParentWnd, m_rtPRect);

	GetWindowRect(&m_rtRect);
	m_rtRect.OffsetRect(+nParentx, +nParenty);
	m_rtRect.OffsetRect(-m_rtPRect.left, -m_rtPRect.top);
}

/*********************************************************************************************************************/
/*********************************************************************************************************************/
//Image Setting
/*********************************************************************************************************************/
/*********************************************************************************************************************/
void CCustomButton::SetImage(UINT nDisabledID, UINT nNormalID, UINT nHoveredID, UINT nPressedID)
{
	CUtility::ImageFromIDResource(nDisabledID, _T("PNG"), m_pImg[0]);
	CUtility::ImageFromIDResource(nNormalID, _T("PNG"), m_pImg[1]);
	CUtility::ImageFromIDResource(nHoveredID, _T("PNG"), m_pImg[2]);
	CUtility::ImageFromIDResource(nPressedID, _T("PNG"), m_pImg[3]);
}

void CCustomButton::SetImage(LPCTSTR pszDisabled, LPCTSTR pszNormal, LPCTSTR pszHovered, LPCTSTR pszPressed)
{
	if ( pszDisabled )
		m_pImg[0] = Image::FromFile( pszDisabled );

	if ( pszNormal )
		m_pImg[1] = Image::FromFile( pszNormal );

	if ( pszHovered )
		m_pImg[2] = Image::FromFile( pszHovered );

	if ( pszPressed )
		m_pImg[3] = Image::FromFile( pszPressed );
}


/*********************************************************************************************************************/
/*********************************************************************************************************************/
//String Font Setting
/*********************************************************************************************************************/
/*********************************************************************************************************************/
void CCustomButton::SetWindowTextW(LPCTSTR lpszString)
{
	CButton::SetWindowTextW(lpszString);
	Invalidate();
}

void CCustomButton::SetFontSize(int nSize)
{
	if(nSize > 0)
		m_nFontSize = nSize;
}

void CCustomButton::SetFontStyle(UINT nStyle)
{
	if(nStyle >= 0)
		m_nFontStyle = nStyle;
}

void CCustomButton::SetFontColor(int nR, int nG, int nB)
{
	if(m_pFontBrush!=NULL){ delete m_pFontBrush;  m_pFontBrush = NULL; }
	m_pFontBrush = new SolidBrush(Color(247, nR, nG, nB));	

}
void CCustomButton::SetFontFamily(CString strFontFamilyName)
{
	if(!strFontFamilyName.IsEmpty())
	{
		if(m_pFontFamily!=NULL){ delete m_pFontFamily;  m_pFontFamily=NULL; }
		m_pFontFamily = new FontFamily(strFontFamilyName);
	}
}

void CCustomButton::CreateFont()
{
	//----------------------new FontFamily
	if(m_pFontFamily == NULL)
		m_pFontFamily = new Gdiplus::FontFamily(L"Arial");

	if(FontFamilyNotFound == m_pFontFamily->GetLastStatus())
	{
		if (this->m_hWnd)
		{
			CFont *pCurFont = this->GetFont();  LOGFONT LgFong;  pCurFont->GetLogFont(&LgFong);
			if(m_pFontFamily){ delete m_pFontFamily;  m_pFontFamily = NULL;  m_pFontFamily = new Gdiplus::FontFamily(LgFong.lfFaceName); }
		}
	}
	
	//----------------------new Font
	if(m_pFont == NULL)
		m_pFont = new Gdiplus::Font(m_pFontFamily,(Gdiplus::REAL)m_nFontSize, m_nFontStyle, m_enFontUnit);

	//----------------------new StringFormat
	if(m_strTextFormat == NULL)
	{
		m_strTextFormat = new StringFormat;
		m_strTextFormat->SetAlignment(StringAlignmentCenter);
		m_strTextFormat->SetLineAlignment(StringAlignmentCenter);
		m_strTextFormat->SetTrimming(StringTrimmingEllipsisCharacter);
	}

	//----------------------new FontBrush
	if (m_pFontBrush == NULL)
		m_pFontBrush = new SolidBrush(Color(247, 247, 247, 247));	
}

/*********************************************************************************************************************/
/*********************************************************************************************************************/
//UI Drawing
/*********************************************************************************************************************/
/*********************************************************************************************************************/
void CCustomButton::DrawItem(LPDRAWITEMSTRUCT di)
{
	if(di->itemState & ODS_SELECTED)
		m_nState = BTN_PRESSED;

	DrawUI();
}

void CCustomButton::DrawUI()
{
	BitBlt(m_hDrawDC, m_rtRect.left, m_rtRect.top, m_rtRect.Width(), m_rtRect.Height(), m_hBgDC, m_rtRect.left, m_rtRect.top, SRCCOPY);

	Graphics graphics( m_hDrawDC );
	graphics.DrawImage(m_pImg[m_nState], m_rtRect.left, m_rtRect.top, m_rtRect.Width(), m_rtRect.Height());

	GetWindowText(m_strText);
	if(!m_strText.IsEmpty())
	{
		CreateFont();
		graphics.DrawString(m_strText.GetBuffer(), -1, m_pFont, 
			                RectF((Gdiplus::REAL)m_rtRect.left,    (Gdiplus::REAL)m_rtRect.top,
			                      (Gdiplus::REAL)m_rtRect.Width(), (Gdiplus::REAL)m_rtRect.Height()),
			                m_strTextFormat, m_pFontBrush);
	}

	CUtility::UpdateWindow(m_hPParentWnd, m_hDrawDC, m_rtPPRect);
}

BOOL CCustomButton::ShowWindow(int nCmdShow)
{
	switch(nCmdShow)
	{
	case SW_SHOW:
		Invalidate();
		break;
	case SW_HIDE:
		BitBlt(m_hDrawDC, m_rtRect.left, m_rtRect.top, m_rtRect.Width(), m_rtRect.Height(), m_hBgDC, m_rtRect.left, m_rtRect.top, SRCCOPY);
		CUtility::UpdateWindow(m_hPParentWnd, m_hDrawDC, m_rtPPRect);
		break;
	default:
		break;
	}

	return CButton::ShowWindow(nCmdShow);
}
/*********************************************************************************************************************/
/*********************************************************************************************************************/
//UI Message
/*********************************************************************************************************************/
/*********************************************************************************************************************/
BEGIN_MESSAGE_MAP(CCustomButton, CButton)
	ON_WM_DRAWITEM()
	ON_WM_MOUSEMOVE()
	ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
	ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)
	ON_WM_ERASEBKGND()
END_MESSAGE_MAP()

void CCustomButton::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
		TRACKMOUSEEVENT tme;
		tme.cbSize = sizeof(tme);
		tme.hwndTrack = m_hWnd;
		tme.dwFlags = TME_LEAVE|TME_HOVER;
		tme.dwHoverTime = 1;
		_TrackMouseEvent(&tme);

	CButton::OnMouseMove(nFlags, point);
}

LRESULT CCustomButton::OnMouseLeave(WPARAM wparam, LPARAM lparam)
{
	if(m_nState != BTN_DISABLED)
		SetState(1);
	return 0;
}

LRESULT CCustomButton::OnMouseHover(WPARAM wparam, LPARAM lparam) 
{
	if(m_nState != BTN_DISABLED)
		SetState(2);
	return 0;
}

BOOL CCustomButton::OnEraseBkgnd(CDC* pDC)
{
	return TRUE;
}

void CCustomButton::PreSubclassWindow()
{
	// TODO: Add your specialized code here and/or call the base class
	if(!IsWindowEnabled())
		m_nState = BTN_DISABLED;

	ModifyStyle(0, BS_OWNERDRAW);
	CButton::PreSubclassWindow();
}
